﻿package GFlash
{
	import flash.media.*;
	import flash.utils.*;
	import flash.events.*;
	import flash.system.*;
	import flash.net.FileReference;

	public class GMicroPhone
	{
		private var gMicPhone:Microphone;
		
		private var gMicEncodeBuffer:ByteArray;
		private var gMicOriginalBuffer:ByteArray;
		
		private var gIsRecorded:Boolean = false;
		
		private static const RIFF:String = "RIFF";	
		private static const WAVE:String = "WAVE";	
		private static const FMT:String = "fmt ";	
		private static const DATA:String = "data";	
		
		private var gAudioBits:int=16;
		private var gAudioRate:int=44100;
		private var gAudioChannel:int=1;

		public function GMicroPhone(level:Number=0.0,loopBack:Boolean=false,gainStrength:Number=100.0)
		{
			// constructor code
			gMicPhone = Microphone.getMicrophone();//获取可用的麦克风设备
			if (gMicPhone)
			{
				trace("Enable MicroPhone");
				//申请权限air请使用gMicPhone.requestPermission();
				Security.showSettings('2');
				//设置触发Level，0一直触发;
				gMicPhone.setSilenceLevel(level);
				//是否一边录制一边播放;
				gMicPhone.setLoopBack(loopBack);
				//麦克风放大信号的程度
				gMicPhone.gain = gainStrength;
				gMicPhone.rate=44;
				//默认使用使用音频编解码器的回音抑制功能;
				gMicPhone.setUseEchoSuppression(true);

				gMicOriginalBuffer=new ByteArray();
				gMicEncodeBuffer=new ByteArray()

				gMicPhone.addEventListener(SampleDataEvent.SAMPLE_DATA, OnSampleData);
				
				StartRecord();
			}
		}

		private function OnSampleData(e:SampleDataEvent):void
		{
			while(e.data.bytesAvailable > 0)
			{
				if (gIsRecorded)
				{
					gMicOriginalBuffer.writeFloat(e.data.readFloat());
				}
				else
				{
					e.data.readFloat();
				}
			}
		}
		private function EncodeAudio()
		{
			var gTempBuffer:ByteArray=new ByteArray();
			gTempBuffer.endian = Endian.LITTLE_ENDIAN;
			gTempBuffer.length = 0;
			
			gMicOriginalBuffer.position=0;//重置起点
			var _volume:Number=1.0;
			while(gMicOriginalBuffer.bytesAvailable) 
			{
				gTempBuffer.writeShort( gMicOriginalBuffer.readFloat() * (0x7fff * _volume));
			}
			///////////////wav格式声音
			gMicEncodeBuffer.length = 0;
			gMicEncodeBuffer.endian = Endian.LITTLE_ENDIAN;
			gMicEncodeBuffer.writeUTFBytes(GMicroPhone.RIFF);
			gMicEncodeBuffer.writeInt( uint(gTempBuffer.length + 44 ) );
			gMicEncodeBuffer.writeUTFBytes(GMicroPhone.WAVE);
			gMicEncodeBuffer.writeUTFBytes(GMicroPhone.FMT);
			gMicEncodeBuffer.writeInt( uint( 16 ) );
			gMicEncodeBuffer.writeShort( uint( 1 ) );
			gMicEncodeBuffer.writeShort( gAudioChannel );
			gMicEncodeBuffer.writeInt( gAudioRate );
			gMicEncodeBuffer.writeInt( uint( gAudioRate * gAudioChannel * ( gAudioBits >> 3 ) ) );
			gMicEncodeBuffer.writeShort( uint( gAudioChannel * ( gAudioBits >> 3 ) ) );
			gMicEncodeBuffer.writeShort( gAudioBits );
			gMicEncodeBuffer.writeUTFBytes(GMicroPhone.DATA);
			gMicEncodeBuffer.writeInt( gTempBuffer.length );
			gMicEncodeBuffer.writeBytes( gTempBuffer );
			gMicEncodeBuffer.position = 0;
		}
		//////////////////////
		public function StartRecord():void
		{
			if (! gIsRecorded)
			{
				gIsRecorded = true;
			}
		}
		public function StopRecord():void
		{
			if (gIsRecorded)
			{
				gIsRecorded = false;
				EncodeAudio();
			}
		}
		public function SaveRecord():void
		{
			var fileReference:FileReference = new FileReference();
			if(!gIsRecorded&&gMicEncodeBuffer.length!=0)
			{
				fileReference.save(gMicEncodeBuffer,'Saved.wav');
			}
		}
		public function GetActivity():Number
		{
			return gMicPhone.activityLevel;
		}
		/////////。。。。。。。。。。。。。。。end
	}

}